package com.example.security1.service;

import com.example.security1.entity.Role;
import com.example.security1.exe.MyUserNameNotFoundException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class MyUserService implements UserDetailsService {
    @Resource
    private RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("进入");
        Role role= roleService.queryByname(username);
        System.out.println(username);
        if (role==null){
            System.out.println("用户不存在");
            throw new MyUserNameNotFoundException("用户名不存在");
        }else {
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            System.out.println(role.getPwd());

            User user = new User(username,role.getPwd(),authorities);
            return user;
        }
    }
}
